package com.cn.boot.student.service.rbac.impl;

import com.cn.boot.student.service.common.impl.RedisServiceImpl;
import com.cn.boot.student.util.CommonResult;
import com.cn.boot.student.entity.PermissionEntity;
import com.cn.boot.student.mapper.PermissionMapper;
import com.cn.boot.student.service.rbac.IPermissionService;
import com.cn.boot.student.util.UploadFile;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;

/**
 * 权限服务逻辑层
 */
@Service
@Transactional
@CacheConfig(cacheNames = "permission")
public class PermissionServiceImpl implements IPermissionService {
    @Autowired
    private PermissionMapper permissionMapper;
    @Autowired
    private RedisServiceImpl redis;

    /**
     * 查询所有
     */
    @Override
    public CommonResult findAll(PermissionEntity permissionEntity) {
        //得到查询总数
        Integer count = permissionMapper.getCount(permissionEntity);
        //分页参数
        PageHelper.startPage(permissionEntity.getPageNum(), permissionEntity.getPageSize());
        List<PermissionEntity> list = permissionMapper.findAll(permissionEntity);
        PageInfo<PermissionEntity> pageInfo = new PageInfo<>(list);
        HashMap<String,Object> map = new HashMap<>();
        map.put("count",count);
        map.put("pageInfo",pageInfo);
        return CommonResult.success(map);
    }

    /**
     * 模糊查询、分页
     *
     * @param permissionEntity
     * @return
     */
    @Override
    public CommonResult fuzzyQuery(PermissionEntity permissionEntity) {
//        PageHelper.startPage(Tool.toPageNum(pageParams.getPageNum()), Tool.toPageSize(pageParams.getPageSize()));
        Page<PermissionEntity> list = permissionMapper.fuzzyQuery(permissionEntity);
        return CommonResult.success(list);
    }



    /**
     * 添加数据
     *
     * @param permissionEntity
     */
    @Override
    public CommonResult add(PermissionEntity permissionEntity) {
        //断言实体类不为空
        assert permissionEntity != null;
        //调用文件上传工具类
        String string = UploadFile.upload(permissionEntity.getFile());
        //存入本地项目中
        if (string != null) {
            permissionEntity.setIcon(string);
        }
        Integer result = permissionMapper.add(permissionEntity);
        if (result == 0) {
            return CommonResult.failed("新增失败！");
        }
        return CommonResult.success(result, "新增成功！");
    }

    /**
     * 更新数据
     *
     * @param permissionEntity
     */
    @Override
    public CommonResult update(PermissionEntity permissionEntity) {
        //断言实体类不为空
        assert permissionEntity != null;
        //调用文件上传工具类
        String string = UploadFile.upload(permissionEntity.getFile());
        //存入本地项目中
        if (string != null) {
            permissionEntity.setIcon(string);
        }
        Integer result = permissionMapper.update(permissionEntity);
        if (result == 0) {
            return CommonResult.failed("更新失败！");
        }
        return CommonResult.success(result, "更新成功！");
    }

    /**
     * 删除数据
     *
     * @param id
     */
    @Override
    public CommonResult del(Integer id) {
        Integer result = permissionMapper.del(id);
        if (result == 0) {
            return CommonResult.failed("删除失败！");
        }
        return CommonResult.success(result, "删除成功！");
    }

    /**
     * 批量删除
     *
     * @param ids
     */
    @Override
    public CommonResult dels(List<Integer> ids) {
        ids.stream().forEach(permissionMapper::del);
        //返回删除条数
        return CommonResult.success(ids.size(), "成功删除" + ids.size() + "条数据");
    }

    /**
     * 根据id查询权限
     *
     * @param id
     */
    @Override
    public CommonResult findById(Integer id) {
        PermissionEntity permissionEntity = permissionMapper.findById(id);
        return CommonResult.success(permissionEntity);
    }


}
